package com.xiao.sulinguser.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.xiao.sulingpublic.to.MemberTo;
import com.xiao.sulingpublic.utils.PageUtils;
import com.xiao.sulingpublic.utils.R;
import com.xiao.sulinguser.entity.MemberEntity;
import com.xiao.sulinguser.service.MemberService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Map;




/**
 * 会员
 * @author wen
 * @email 1322127758@qq.com
 * @date 2022-03-11 15:01:06
 */
@RestController
@RequestMapping("sulinguxqg/member")
public class MemberController {
    @Autowired
    private MemberService memberService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    //@RequiresPermissions("sulinguxqg:member:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = memberService.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    //@RequiresPermissions("sulinguxqg:member:info")
    public R info(@PathVariable("id") Long id){
		MemberEntity member = memberService.getById(id);

        return R.ok().put("member", member);
    }

    /**
     * 通过member获取用户信息
     */
    @RequestMapping("/select/userData")
    //@RequiresPermissions("sulinguxqg:member:info")
    public R getUserDataByUsername(@RequestParam("username") String username){
        MemberEntity member = memberService.getOne(new QueryWrapper<MemberEntity>().eq("username",username));

        member.setPassword("");
        return R.ok().setData("member", member);
    }

    /**
     * 通过member获取用户头像
     */
    @RequestMapping("/select/user_header")
    public R getHeaderUrl(@RequestParam("username") String username){
        MemberEntity member = memberService.getOne(new QueryWrapper<MemberEntity>().eq("username",username));
        if(null == member){
            return R.error();
        }
        return R.ok().put("header", member.getHeader());
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    //@RequiresPermissions("sulinguxqg:member:save")
    public R save(@RequestBody MemberEntity member){
		memberService.save(member);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    //@RequiresPermissions("sulinguxqg:member:update")
    public R update(@RequestBody MemberEntity member){
		memberService.updateById(member);

        return R.ok();
    }

    /**
     * 通过用户号修改数据
     */
    @RequestMapping("/update/byUsername")
    public R updateByUsername(@RequestBody MemberTo memberTo) throws Exception {
        MemberEntity member = new MemberEntity();
        BeanUtils.copyProperties(memberTo, member);
        boolean b = memberService.update(member, new QueryWrapper<MemberEntity>().eq("username", member.getUsername()));

        if(b){
            return R.ok();
        }
        throw new Exception("修改失败");
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    //@RequiresPermissions("sulinguxqg:member:delete")
    public R delete(@RequestBody Long[] ids){
		memberService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
